package com.study.jyg.rxjava2study.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.view.View;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.base.BasicActivity;
import com.study.jyg.rxjava2study.bean.MultiGroupHistogramGroupData;
import com.study.jyg.rxjava2study.widget.HistogramView;
import com.study.jyg.rxjava2study.widget.MultiGroupHistogramView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ZhuZhuangTuActivity extends BasicActivity {


//    手机号 序列号  mac  imei  蓝牙

    private OrientationEventListener orientationEventListener;
    private MultiGroupHistogramView multiView;

    @Override
    protected int getLayout() {
        return R.layout.activity_zhu_zhuang_tu;
    }

    @Override
    protected void initView() {
        multiView = findViewById(R.id.multiView);
        HistogramView view = findViewById(R.id.histogramView);
        List<Long> longs = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            longs.add(0L + i);
            strings.add(String.valueOf(i + 1));
        }
        view.updateThisData(longs, strings);

        initZZT();
//        initOrientationListener();
    }

    private void initZZT() {
        Random random = new Random();
        int groupSize = random.nextInt(10) + 10;
        List<MultiGroupHistogramGroupData> groupDataList = new ArrayList<>();
        for (int i = 0; i < groupSize; i++) {

            List<MultiGroupHistogramGroupData.MultiGroupHistogramChildData> childDataList = new ArrayList<>();

            MultiGroupHistogramGroupData groupData = new MultiGroupHistogramGroupData();
            groupData.setGroupName("第" + (i + 1) + "组");

            MultiGroupHistogramGroupData.MultiGroupHistogramChildData childData1 = new MultiGroupHistogramGroupData.MultiGroupHistogramChildData();
            childData1.setSuffix("cm");
            childData1.setValue(random.nextInt(50) + 51);
            childDataList.add(childData1);

            if (i%2 == 0) {
                MultiGroupHistogramGroupData.MultiGroupHistogramChildData childData2 = new MultiGroupHistogramGroupData.MultiGroupHistogramChildData();
                childData2.setSuffix("cm");
                childData2.setValue(random.nextInt(50) + 51);
                childDataList.add(childData2);
            }
            groupData.setChildDataList(childDataList);
            groupDataList.add(groupData);
        }
        multiView.setDataList(groupDataList);
        int[] color1 = new int[]{Color.parseColor("#FFD100"), Color.parseColor("#FFD100"/*"#FF3300"*/)};

        int[] color2 = new int[]{Color.parseColor("#1DB890"), Color.parseColor("#1DB890"/*"#4576F9"*/)};
        multiView.setHistogramColor(color1, color2);


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (orientationEventListener != null) {
            orientationEventListener.enable();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (orientationEventListener != null) {
            orientationEventListener.disable();
        }
    }

    private void initOrientationListener() {
        orientationEventListener = new OrientationEventListener(this) {
            @Override
            public void onOrientationChanged(int orientation) {
                int screenOrientation = getResources().getConfiguration().orientation;
                if (orientation > 315 || orientation < 45 && orientation > 0) {
                    if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    }
                } else if (orientation > 45 && orientation < 135) {
                    if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                    }
                } else if (orientation > 135 && orientation < 225) {
//                    if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
//                        LogUtil.e("kkkkkkkk: " + orientation);
//                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
//                    }
                } else if (orientation > 225 && orientation < 315) {
                    if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    }
                }
            }
        };
    }
}